import moment from 'moment';
import { TRMessage } from '#/components';
import { LOCKED_TYPE, PROGARM_STATUS, SUBMIT_TYPE, TRADE_STRATEGY } from '../constant/enums';
import { CREATE_PROGRAM_EXIST_WARNING_MESSAGE } from '../constant/message';
import { arrHasElement } from './element';

/**
 * 校验是否有创建的方案存在
 * @param {Array} programList 方案列表
 * @param {Function} func 要执行的方法
 */
export const createProgramExistValid = (programList, func) => {
  if (programList.find(({ status }) => status === PROGARM_STATUS.CREATE)) {
    TRMessage.warning(CREATE_PROGRAM_EXIST_WARNING_MESSAGE);
  } else {
    func();
  }
};

/**
 * 判断一下选择的场站是否改变
 * @param {Array} originUnitList
 * @param {Array} selectedUnitList
 */
export const selectedUnitsHasModified = (originUnitList, selectedUnitList) => {
  if (
    !arrHasElement(originUnitList) ||
    !arrHasElement(selectedUnitList) ||
    originUnitList.length !== selectedUnitList.length
  ) {
    return false;
  }
  let originUnitIdInfo = {};
  originUnitList.forEach(({ unitId }) => (originUnitIdInfo[unitId] = true));
  return selectedUnitList.filter(({ unitId }) => !originUnitIdInfo[unitId]).length == 0;
};

/**
 * 校验时段电量是否合法
 * @param {Array} hourOrderList 要校验的时段
 * @param {Array} unitList 方案选择的场站
 * @param {Object} powerLimitInfo 场站的电量限额信息
 */
export const validPower = (hourOrderList, unitList, powerLimitInfo) => {
  let list = [],
    hourOrderValid = (powerLimit, type, power, capacity) => {
      let { FIXED, INSTALLED_CAPACITY, TRADE_LIMIT } = SUBMIT_TYPE;
      switch (type) {
        case FIXED:
          return power <= powerLimit;
        case INSTALLED_CAPACITY:
          return power * capacity <= powerLimit;
        case TRADE_LIMIT:
          return true;
        default:
          return true;
      }
    };
  hourOrderList.forEach(({ tradeStrategyType, submitType, hourOrder, power }) => {
    let { SELL, BUY } = TRADE_STRATEGY;
    switch (tradeStrategyType) {
      case SELL:
      case BUY:
        let exceedTitles = [];
        unitList.forEach(({ unitId, unitTitle }) => {
          let { sellPowerLimit, buyPowerLimit, unitCapacity } = powerLimitInfo[unitId][hourOrder],
            powerLimit = tradeStrategyType === SELL ? sellPowerLimit : buyPowerLimit;
          if (!hourOrderValid(powerLimit, submitType, power, unitCapacity)) {
            exceedTitles.push(unitTitle);
          }
        });
        if (exceedTitles.length) {
          list.push({ exceedTitles, hourOrder });
        }
    }
  });

  return { list, result: !arrHasElement(list) };
};

/**
 * 校验时段交易方向是否合法
 * @param {*} hourOrderList
 * @param {*} unitLockedInfo
 * @param {*} unitList
 */
export const validTradeStrategy = (hourOrderList, unitLockedInfo, unitList) => {
  let validArr = [];
  hourOrderList.forEach(({ tradeStrategyType, hourOrder }) => {
    let arr = [];
    unitList.forEach(({ unitId, unitTitle }) => {
      let lockedType = unitLockedInfo[unitId][hourOrder]?.lockedTradeType;
      switch (tradeStrategyType) {
        case TRADE_STRATEGY.SELL:
          if (lockedType === LOCKED_TYPE.BUY) arr.push(unitTitle);
          break;
        case TRADE_STRATEGY.BUY:
          if (lockedType === LOCKED_TYPE.SELL) arr.push(unitTitle);
          break;
        default:
          break;
      }
    });
    if (arrHasElement(arr)) {
      validArr.push({ hourOrder, unitTitles: arr.join('、') });
    }
  });
  return { list: validArr, result: !arrHasElement(validArr) };
};

/**
 * 立即提交时，校验时间是否合法
 * @param {moment} momentTime 交易中心时间
 */
export const validSubmitTime = (
  momentTime,
  { startTimeFirst, endTimeFirst, startTimeSecond, endTimeSecond },
) =>
  momentIsBetween(startTimeFirst, endTimeFirst, momentTime) ||
  momentIsBetween(startTimeSecond, endTimeSecond, momentTime);

export const momentIsBetween = (startTime, endTime, momentTime) =>
  momentTime.isAfter(moment(startTime)) && momentTime.isBefore(moment(endTime));
